package com.whoru.app;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import com.whoru.logica.*;


public class Mapa extends MapActivity {
	
	 private LocationManager mlocManager;
	 private LocationListener mlocListener;

	 private MapView mapView;
	 private MapController mc;
	 
	 private List<Overlay> mapOverlays;
	 private Marcadores itemizedoverlay;
	 private OverlayItem overlayitem;
	 
 @Override
 protected void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.maps);
    
    mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);
    mc = mapView.getController();
    
    mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    mlocListener = new MyLocationListener();
    mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
    
    Connection con = new Connection("http://172.17.225.161/txus.php");
    con.AddParam("method", "userGet");
    con.AddParam("id", "1");
    
    try {
    	con.Execute(1);
    } catch (Exception e) {
        e.printStackTrace();
    }
     
    String response = con.getResponse();
    JSONObject json_data;
    String name="default";
    String situ="default";
    int lat=43065000;
    int lon=-24868600;

    AlertDialog.Builder dialog;
    
    try{
        JSONArray jArray = new JSONArray(response);
        dialog = new AlertDialog.Builder(this);
        for(int i=0;i<jArray.length();i++){
                json_data = jArray.getJSONObject(i);
                dialog = new AlertDialog.Builder(this);
        		name=json_data.getString("name");
        		//situ=json_data.getString("situ");
        		//lat=json_data.getInt("lat");
        		//lon=json_data.getInt("lon");
        		dialog.setTitle(name);
        		dialog.setMessage(situ+"\n\nLongitud: "+lon+"\nLatidut: "+lat);
        		dialog.show();
        }
	}catch(JSONException e){
		dialog = new AlertDialog.Builder(this);
		dialog.setTitle("ERROR");
		dialog.setMessage(e.toString());
		dialog.show();
	}
 
    mapOverlays = mapView.getOverlays();
    Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
    itemizedoverlay = new Marcadores(drawable,this);
    
    GeoPoint point = new GeoPoint(lat,lon);
    overlayitem = new OverlayItem(point, name, situ);
    
    itemizedoverlay.addOverlay(overlayitem);
    mapOverlays.add(itemizedoverlay);
    mc.animateTo(point);
    mc.setZoom(14);
 }

 @Override
 protected boolean isRouteDisplayed() {
   return false;
 }
 
 public class MyLocationListener implements LocationListener {

     public void onLocationChanged(Location loc) {
         if (loc != null) {                
             //Toast.makeText(getBaseContext(), 
               //  "Location changed : Lat: " + loc.getLatitude() + 
                 //" Lng: " + loc.getLongitude(), 
                 //Toast.LENGTH_SHORT).show();
             
             GeoPoint p = new GeoPoint(
                     (int) (loc.getLatitude() * 1E6), 
                     (int) (loc.getLongitude() * 1E6));
             
             overlayitem = new OverlayItem(p, "Hola!", "I'm in somewhere!");
             itemizedoverlay.addOverlay(overlayitem);
     	    mapOverlays.add(itemizedoverlay);
     	    
             mc.animateTo(p);
             mc.setZoom(16);                
             mapView.invalidate();
         }
 	}
 	
 	public void onProviderDisabled(String provider) {
 		Toast.makeText( getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT ).show();
 	}

 	public void onProviderEnabled(String provider) {
 		Toast.makeText( getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
 	}

 	public void onStatusChanged(String provider, int status, Bundle extras) {
 		// TODO Auto-generated method stub

 	}
 }
 
}
